package com.controller;

import cn.hutool.core.bean.BeanUtil;
import com.domain.dto.UserFormDTO;
import com.domain.po.User;
import com.domain.vo.UserVO;
import com.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {


    private final IUserService userService;

    @ApiOperation("新增用户接口")
    @PostMapping
    public void saveUser(@RequestBody UserFormDTO userDTO){
        //把DTO拷贝到PO
        User user = BeanUtil.copyProperties(userDTO, User.class);
        userService.save(user);
    }


    @ApiOperation("删除用户接口")
    @DeleteMapping("{id}")
    public void deleteUserById(@ApiParam("用户id")@PathVariable("id")Long id){
        userService.removeById(id);
    }

    @ApiOperation("根据id查询用户接口")
    @GetMapping("{id}")
    public UserVO queryUserById(@ApiParam("用户id")@PathVariable("id")Long id){
        return userService.queryUserAndAddressById(id);
    }

    @ApiOperation("根据id批量查询用户接口")
    @GetMapping
    public List<UserVO> queryUserByIds(@ApiParam("用户id集合")@RequestParam("ids")List<Long> ids){
        return userService.queryUserAndAddressByIds(ids);
    }

    @ApiOperation("扣减用户余额接口")
    @PutMapping("/{id}/deduction/{money}")
    public void deductBalance(@ApiParam("用户id")@PathVariable("id")Long id,
                              @ApiParam("扣减的余额")@PathVariable("money")Integer money){
        userService.deductBalance(id,money);
    }
}
